<template>
  <div>
    <!-- 头部导航区 -->
    <van-nav-bar
      title="个人信息"
      left-arrow
      @click-left="$router.back()"
    />
    <!-- 头像区 -->
    <input
      ref="file"
      type="file"
      hidden
      @change="onChange"
    >
    <van-cell
      @click="$refs.file.click()"
      title="头像"
      is-link
    >
      <van-image
        round
        :src="user.photo"
      />
    </van-cell>
    <!-- 修改头像区 -->
    <van-popup
      v-model="isShowImg"
      position="bottom"
      :style="{ height: '100%' }"
    >
      <upload-img
        v-if="isShowImg"
        :value="img"
        @close="isShowImg=false"
        @upload="user.photo=$event"
      ></upload-img>
    </van-popup>
    <!-- 昵称区 -->
    <van-cell
      title="昵称"
      is-link
      :value="user.name"
      @click="isShowName=true"
    />
    <!-- 修改昵称区 -->
    <van-popup
      v-model="isShowName"
      position="bottom"
      :style="{ height: '100%' }"
    >
      <upload-name
        v-if="isShowName"
        @close="isShowName=false"
        v-model="user.name"
      ></upload-name>
    </van-popup>
    <!-- 性别区 -->
    <van-cell
      @click="isShowGender=true"
      title="性别"
      is-link
      :value="user.gender===0?'男':'女'"
    >
    </van-cell>
    <van-popup
      v-model="isShowGender"
      position="bottom"
    >
      <upload-gender
        v-if="isShowGender"
        v-model="user.gender"
        @close="isShowGender=false"
      ></upload-gender>
    </van-popup>
    <!-- 生日区 -->
    <van-cell
      @click="isShowBirthday=true"
      title="出生日期"
      is-link
      :value="user.birthday"
    />
    <van-popup
      v-model="isShowBirthday"
      position="bottom"
    >
      <upload-birthday
        v-if="isShowBirthday"
        v-model="user.birthday"
        @close="isShowBirthday=false"
      >
      </upload-birthday>
    </van-popup>
  </div>
</template>

<script>
import { getUserProfile } from '@/api/user'
import uploadName from './components/upload-name'
import uploadGender from './components/upload-gender'
import uploadBirthday from './components/upload-birthday'
import uploadImg from './components/upload-img'
export default {
  created () {
    this.getUserProfile()
  },
  data () {
    return {
      user: {},
      isShowName: false,
      isShowGender: false,
      isShowBirthday: false,
      isShowImg: false,
      img: ''
    }
  },
  methods: {
    async getUserProfile () {
      try {
        const { data: res } = await getUserProfile()
        this.user = res.data
        console.log(res)
      } catch (err) {
        this.$toast.fail('获取信息失败')
      }
    },
    onChange () {
      const file = this.$refs.file.files[0]
      const image = window.URL.createObjectURL(file)
      this.$refs.file.value = ''
      this.img = image
      this.isShowImg = true
    }
  },
  components: {
    uploadName,
    uploadGender,
    uploadBirthday,
    uploadImg
  }
}
</script>

<style lang="less" scoped>
/deep/ .van-nav-bar {
  background: #3296fa;
  .van-nav-bar__title,
  .van-icon {
    color: aliceblue !important;
  }
}
.van-image {
  width: 60px;
  height: 60px;
}
.van-cell {
  height: 100px;
}
</style>
